zoukankan      html  css  js  c++  java
  • LeetCode | Invert Binary Tree

    Invert a binary tree.

         4
       /   
      2     7
     /    / 
    1   3 6   9

    to

         4
       /   
      7     2
     /    / 
    9   6 3   1

    Trivia:
    This problem was inspired by this original tweet by Max Howell:

    Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    //递归解法
    public class Solution {
        public TreeNode invertTree(TreeNode root) {
            
            if(root == null){
                return null;
            }
            
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            
            invertTree(root.left);
            invertTree(root.right);
            
            return root;
        }
    }
    //迭代解法
    //思路就是利用一个容器作为缓冲,每次从容器中取出一个node,交换它的left与right,再将子节点放入容器
    //关键是要保证每个node的left与right都能被交换,而至于容器用queue还是stack都是无所谓的,只要保证容器
    //内的node顺序不乱就行
    public class Solution {
        public TreeNode invertTree(TreeNode root) {
            
            if(root == null){
                return null;
            }
            
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.add(root);
            
            while(!queue.isEmpty()){
                TreeNode curNode = queue.remove();   //返回并移除队头
                TreeNode temp = curNode.left;
                curNode.left = curNode.right;
                curNode.right = temp;                //交换队头的left与right
                
                if(curNode.left != null){            //再把left与right均入队
                    queue.add(curNode.left);
                }
                if(curNode.right != null){           //此处两个入队的顺序是无关紧要的
                    queue.add(curNode.right);        //主要就是每次从队中取出一个node,交换它的left与right
                }                                    //然后在把他的子节点也入队,实现类似递归的过程
            }
            
            return root;
        }
    }
    public class Solution {
        public TreeNode invertTree(TreeNode root) {
            
            if(root == null){
                return null;
            }
            
            Stack<TreeNode> stack = new Stack<TreeNode>();
            stack.push(root);
            
            while(!stack.isEmpty()){
                TreeNode curNode = stack.pop();
                TreeNode temp = curNode.left;
                curNode.left = curNode.right;
                curNode.right = temp;
                
                if(curNode.left != null){
                    stack.push(curNode.left);
                }
                if(curNode.right != null){
                    stack.push(curNode.right);
                }
            }
            
            return root;
        }
    }



  • 相关阅读:
    [Win32]一个调试器的实现(十)显示变量
    [Win32]防止套接字被继承
    [C++]实现委托模型
    [Win32]一个调试器的实现(十一)显示函数调用栈
    [Win32]IP数据报的首部如何定义
    FMECA方法及工程应用
    C#控制台应用程序自动关闭
    ckedit 3.0 配置(一)
    [转]“余则成”教你办公室生存法则20条
    Element UI之Select选择器优化
  • 原文地址:https://www.cnblogs.com/dosmile/p/6444438.html
Copyright © 2011-2022 走看看