diff --git a/BinaryTree.java b/BinaryTree.java index a58a214..b4580f5 100644 --- a/BinaryTree.java +++ b/BinaryTree.java @@ -180,33 +180,33 @@ public TreeNode search(TreeNode root, int key) { * already contain the key. Return the root of the binary search tree. */ // public TreeNode insert(TreeNode root, int key) { - // if (root == null) { - // return new TreeNode(key); - // } - // insertHelper(root, key); - // return root; + // if (root == null) { + // return new TreeNode(key); + // } + // insertHelper(root, key); + // return root; // } // private void insertHelper(TreeNode node, int key) { - // if (node == null || node.key == key) { - // return; - // } - - // if (node.key > key) { - // if (node.left == null) { - // node.left = new TreeNode(key); - // } else { - // insertHelper(node.left, key); - // } - // } else { - // if (node.right == null) { - // node.right = new TreeNode(key); - // } else { - // insertHelper(node.right, key); - // } - // } + // if (node == null || node.key == key) { + // return; + // } + + // if (node.key > key) { + // if (node.left == null) { + // node.left = new TreeNode(key); + // } else { + // insertHelper(node.left, key); + // } + // } else { + // if (node.right == null) { + // node.right = new TreeNode(key); + // } else { + // insertHelper(node.right, key); + // } // } - public TreeNode insert(TreeNode root, int key) { + // } + public TreeNode insert(TreeNode root, int key) { if (root == null) { return new TreeNode(key); } @@ -217,4 +217,172 @@ public TreeNode insert(TreeNode root, int key) { } return root; } + + /** + * Delete In Binary Search Tree + *
+ * Delete the target key K in the given binary search tree if the binary search + * tree contains K. Return the root of the binary search tree. Find your own way + * to delete the node from the binary search tree, after deletion the binary + * search tree's property should be maintained. + */ + public TreeNode deleteTree(TreeNode root, int key) { + if (root == null) { + return null; + } + if (root.key > key) { + root.left = deleteTree(root.left, key); + return root; + } + if (root.key < key) { + root.right = deleteTree(root.right, key); + return root; + } + + if (root.right == null) { + return root.left; + } + if (root.right.left == null) { + root.right.left = root.left; + return root.right; + } + + TreeNode node = root.right; + while (node.left.left != null) { + node = node.left; + } + TreeNode newRoot = node.left; + node.left = newRoot.right; // !!!!IMPORTANT + newRoot.left = root.left; + newRoot.right = root.right; + return newRoot; + } + + /** + * Pre-order Traversal of Binary Tree (iterative) + *
+ * Implement an iterative, pre-order traversal of a given binary tree, return
+ * the list of keys of each node in the tree as it is pre-order traversed.
+ */
+ public List
+ * Implement an iterative, in-order traversal of a given binary tree, return the
+ * list of keys of each node in the tree as it is in-order traversed.
+ */
+ public List
+ * Implement an iterative, post-order traversal of a given binary tree, return
+ * the list of keys of each node in the tree as it is post-order traversed.
+ */
+ public List