Leetcode学习笔记:#897. Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
实现:
private TreeNode iroot;
public TreeNode increasingBST(TreeNode root) {
if(root == null) return null;
TreeNode tmp = iroot = new TreeNode(-1);
inorder(root);
return tmp.right;
}
public void inorder(TreeNode root){
if(root == null) return;
inorder(root.left);
iroot = iroot.right = root;
inorder(root.right);
root.left = null;
}
思路:
中序遍历,遇到右节点设为root,左节点设为Null。