Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
完全没看懂这一题是啥意思。。。完全没看懂这一题再说什么,要做什么,代码也没看懂啊
书上的代码如下:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Main {
public TreeNode UpsideDownBinaryTree(TreeNode root) {
TreeNode p = root;
TreeNode parent = null;
TreeNode parentRight = null;
while(p!=null) {
TreeNode left = p.left;
p.left = parentRight;
parentRight = p.right;
p.right = parent;
parent = p;
p = left;
}
return parent;
}
}