递归--上下翻转二叉树
https://leetcode-cn.com/problems/binary-tree-upside-down/
1 一个节点 返回原节点
2 两个节点 左变上,上变右
3 三个节点,右变左,左变上,上变右
4 >3 左子树已被翻转,则找到左子树最右节点,右变左,上变右
递归 java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode turn(TreeNode root) {
TreeNode temp;
if (root==null||root.left == null) {
return root;
}
else {
temp = turn(root.left);
}
TreeNode tmpNode = temp;
while( tmpNode.right !=null )
{
tmpNode = tmpNode.right;
}
tmpNode.left = root.right;
tmpNode.right = root;
root.left = null;
root.right = null;
return temp;
}
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root==null) return null;
return turn(root);
}
}
==== nice day
浙公网安备 33010602011771号