zoukankan      html  css  js  c++  java
  • Lintcode175-Revert Binary Tree-Easy

    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);
        }
    }
  • 相关阅读:
    Nodejs-原型链污染
    dpwwn-02靶机渗透
    dpwwn-01靶机渗透
    Bulldog1靶机渗透
    php+html实现用户登录退出
    DC4靶机
    vulnhub-Os-hackNos-3
    Linux系统解析XML中文乱问题
    idea添加database
    PL/SQL学习笔记
  • 原文地址:https://www.cnblogs.com/Jessiezyr/p/10661845.html
Copyright © 2011-2022 走看看