[刷题]Insert Node in a Binary Search Tree
2015-03-30 18:50
399 查看
[LintCode]Insert
Node in a Binary Search Tree
Node in a Binary Search Tree
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of the binary search tree. * @param node: insert this node into the binary search tree * @return: The root of the new binary search tree. */ public TreeNode insertNode(TreeNode root, TreeNode node) { // 2015-3-30 if (node == null) { return root; } if (root == null) { return node; } TreeNode temp = root; TreeNode last = null; boolean left = true; while (temp != null) { if (temp.val < node.val) { last = temp; temp = temp.right; left = false; } else if (temp.val > node.val) { last = temp; temp = temp.left; left = true; } else { return root; } } if (left == true) { last.left = node; } else { last.right = node; } return root; } }
相关文章推荐
- Insert Node in a Binary Search Tree
- Lintcode: Insert Node in a Binary Search Tree
- Lintcode85 Insert Node In A Binary Search Tree solution 题解
- lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点
- lintcode-easy-Insert Node in a Binary Search Tree
- lintcode: Insert Node in a Binary Search Tree
- Insert Node in a Binary Search Tree
- #85 Insert Node in a Binary Search Tree
- Insert Node in a Binary Search Tree
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- [Lintcode] Insert Node in a Binary Search Tree
- Lintcode - Remove Node in Binary Search Tree
- LintCode : Remove Node in Binary Search Tree
- Lintcode: Remove Node in Binary Search Tree
- Remove Node in Binary Search Tree 解答
- LintCode "Remove Node in Binary Search Tree"
- [刷题]Remove Node in Binary Search Tree
- lintcode:Remove Node in Binary Search Tree
- LintCode-Remove node in Binary Search Tree