首页 > 其他 > 详细

Lintcode175-Revert Binary Tree-Easy

时间:2019-04-06 16:03:16      阅读:126      评论:0      收藏:0      [点我收藏+]

175. Invert Binary Tree

Invert a binary tree.

Example

Example 1:

Input: {1,3,#}
Output: {1,#,3}
Explanation:
	  1    1
	 /  =>  	3        3

Example 2:

Input: {1,2,3,#,#,4}
Output: {1,3,2,#,4}
Explanation: 
	
      1         1
     / \       /     2   3  => 3   2
       /             4         4

Challenge

Do it in recursion is acceptable, can you do it without recursion?

 
递归法代码:
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    public void invertBinaryTree(TreeNode root) {
        if (root == null) {
            return;
        }
        
        TreeNode temp = root.right;
        root.right = root.left;
        root.left = temp;
        invertBinaryTree(root.left);
        invertBinaryTree(root.right);
    }
}

 

 

Lintcode175-Revert Binary Tree-Easy

原文:https://www.cnblogs.com/Jessiezyr/p/10661845.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!