Invert Binary Tree

package cn.edu.xidian.sselab;

/**
 * title:Invert Binary Tree
 * content:
 * nvert a binary tree.
 *     4
 *   /   \
 *  2     7
 * / \   / \
 *1   3 6   9
 *to
 *     4
 *   /   \
 *  7     2
 * / \   / \
 *9   6 3   1
 */
public class InvertBinaryTree {

/**
     * @author wzy
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

}
    public class TreeNode{
        TreeNode left;
        TreeNode right;
        int val;
        TreeNode(int x){
            val = x;
        }
    }
    //思路,这个题一开始是做今日头条笔试的时候的题,当时做的特别麻烦,还没有做出来,现在还是没有做出来,看了结果分析了一下
    //以后遇到树的题目,第一反应一定要有:递归、递归、递归,利用递归进行遍历
    //这个题一开始理解错了,现在重新强调一下,递归终止的条件是当遇到空节点或者叶子节点时,不再进行交换,直接返回该节点,对于其他节点
    //分别交换他的左子树和右子树,然后将交换过后的左子树赋给右节点,右子树赋给左节点,左子树赋给有节点,
    //这里用到的是后序遍历,从下而上的交换;如果是先序遍历的话,要从上而下进行交换。
    public TreeNode invertTree(TreeNode root){
        if(root == null || (root.left == null && root.right == null))
            return root;
        TreeNode newLeft = invertTree(root.right);
        TreeNode newRight = invertTree(root.left);
        root.left = newLeft;
        root.right = newRight;
        return root;
    }

}

时间: 2025-01-16 08:15:01

Invert Binary Tree的相关文章

leetcode 226 Invert Binary Tree

题目连接 https://leetcode.com/problems/invert-binary-tree/ Invert Binary Tree Description Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tre

[LeetCode][JavaScript]Invert Binary Tree

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

LeetCode226:Invert Binary Tree 二叉树反转

题目 226. Invert Binary Tree QuestionEditorial Solution My Submissions Total Accepted: 109341 Total Submissions: 230799 Difficulty: Easy Invert a binary tree.      4    /     2     7  / \   / 1   3 6   9 to      4    /     7     2  / \   / 9   6 3   1

226. Invert Binary Tree(C++)

226. Invert Binary Tree Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 代码实现 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), l

【07_226】Invert Binary Tree

Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy 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 enginee

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So

leetcode_226题——Invert Binary Tree(队列,广度优先搜索)

Invert Binary Tree Total Accepted: 22352 Total Submissions: 62065My Submissions Question Solution 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%

【Invert Binary Tree】cpp

题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solution 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:

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d