LeetCode -- Invert Binary Tree

Question:

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.

Analysis:

问题描述:给出一个二叉树,然后将它的左右子树置换。

思路:这种题目首先应该反射到递归。用递归有两个条件:return 语句的书写和递归语句的书写。在这道题目中,只要一个节点有任何左节点或者右节点,则应该将他们置换。因此只要非空就置换

Answer:

/**
 * 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 root;
      TreeNode t = root.left;
      root.left = root.right;
      root.right = t;
      invertTree(root.left);
      invertTree(root.right);
      return root;
    }
}
时间: 2024-08-01 10:45:17

LeetCode -- Invert Binary Tree的相关文章

LeetCode——Invert Binary Tree

Description: Invert a binary tree. 4    /    \  2      7 /  \    /   \1   3   6   9 to 4 / 7 2 / \ / 9 6 3 1递归invert就好了. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x

[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 w

LeetCode Invert Binary Tree 反转二叉树

思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. 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), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solutio

LeetCode —— Invert Binary Tree

struct TreeNode* invertTree(struct TreeNode* root) { if ( NULL == root ) { return NULL; } if ( NULL == root->left && NULL == root->right ) { //叶子节点 return root; } //交换左右子节点 struct TreeNode * pTreeNodeTmp = root->left; root->left = root

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

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

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

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 { /**