zoukankan      html  css  js  c++  java
  • [Algorithm] 226. Invert Binary Tree

    Invert a binary tree.

    Example:

    Input:

         4
       /   
      2     7
     /    / 
    1   3 6   9

    Output:

         4
       /   
      7     2
     /    / 
    9   6 3   1
    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} root
     * @return {TreeNode}
     */
    
    // Using BFS to go though all the node
    // if node has children, swap the children
    var invertTree = function(root) {
       let stack = [root];
        
        while (stack.length !== 0) {
            let crt = stack.shift()
            
            if (crt != undefined) {
                var temp = crt.right;
                crt.right = crt.left
                crt.left = temp;
    
                stack.push(crt.left);        
                stack.push(crt.right);
            }
        }
        
        return root
    };
  • 相关阅读:
    [Violet]蒲公英
    CF535-Div3
    逛公园
    exgcd
    线段树套线段树
    Luogu P2730 魔板 Magic Squares
    fhqtreap
    AtCoder Beginner Contest 115
    关于这个博客
    智障错误盘点
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12292648.html
Copyright © 2011-2022 走看看