标签:
递归
public TreeNode insertNode(TreeNode root, TreeNode node) { if (root == null) { return node; } if (root.val >= node.val) { root.left = insertNode(root.left, node); } if (root.val < node.val) { root.right = insertNode(root.right, node); } return root; }
非递归
public TreeNode insertNode(TreeNode root, TreeNode node) { if (root == null) { return node; } TreeNode cur = root; TreeNode last = null; while (cur != null) { last = cur; if (cur.val > node.val) { cur = cur.left; } else { cur = cur.right; } } if (last != null) { if (last.val > node.val) { last.left = node; } else { last.right = node; } } return root; }
标签:
原文地址:http://www.cnblogs.com/hesier/p/5573859.html