首页 > 其他 > 详细

Lintcode 175 Invert Binary Tree

时间:2016-03-05 11:27:38      阅读:124      评论:0      收藏:0      [点我收藏+]

技术分享

I did it in a recursive way. There is another iterative way to do it. I will come back at it later.

/**
 * 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
     */
     
    // method 1: recursion
    // 注意交换的是node,不是int value
    public void invertBinaryTree(TreeNode root) {
        if (root == null)
            return;
        
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        
        invertBinaryTree(root.left);
        invertBinaryTree(root.right);
    }
}

 

Lintcode 175 Invert Binary Tree

原文:http://www.cnblogs.com/wenchan/p/5244306.html

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