delete an extra edge from BST

Given a BST, remove an extra edge to make it BST.

    7

   / \

  5   9

 /  \ /  

3    8

 1 public void deleteEdge(TreeNode root) {
 2     if(root == null) return;
 3     root = dfs(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
 4 }
 5 private TreeNode dfs(TreeNode root, int left, int right) {
 6     if(root == null) return null;
 7     if(root.val <= left || root.val >= right) return null;
 8     root.left = dfs(root.left, left, root.val);
 9     root.right = dfs(root.right, root.val, right);
10     return root;
11 }

 

posted @ 2019-06-22 11:25  北叶青藤  阅读(309)  评论(0编辑  收藏  举报