-
Notifications
You must be signed in to change notification settings - Fork 5
TREE
YaoYilin edited this page Jun 13, 2018
·
2 revisions
翻转一棵二叉树。
示例:
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
输出:
4
/ \
7 2
/ \ / \
9 6 3 1
public TreeNode InvertTree(TreeNode root)
{
if(root == null)
return null;
var tree = root.left;
root.left = root.right;
root.right = tree;
InvertTree(root.left);
InvertTree(root.right);
return root;
}
主要的思想就是缓存左节点,把右节点赋给左节点,缓存再赋给左节点,有点类似交换两个变量a、b的值。然后继续向子节点递归即可。
GOTO HOME ● BIT OPERATION ● LINKED LIST ● MATH ● STACK ● TREE