leetcode:Inver Binary Tree

Posted 自朗活

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode:Inver Binary Tree相关的知识,希望对你有一定的参考价值。

1、

Invert a binary tree.

  1         1
 / \       / 2   3  => 3   2
   /         4         4

2、
/**
 * 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.left;
        root.left = root.right;
        root.right = temp;
        //左右再次遍历,逐一遍历下去
        invertBinaryTree(root.left);
        invertBinaryTree(root.right);
 
    }
}

 

 

以上是关于leetcode:Inver Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 110. Balanced Binary Tree

109. Convert Sorted List to Binary Search Tree

leetcode 94. Binary Tree Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

Binary Tree Traversal In Three Ways In Leetcode

LeetCode 501. Find Mode in Binary Search Tree(寻找二叉查找树中出现次数最多的值)