11.二叉搜索树的插入

74 阅读1分钟

image.png

题解:

 TreeNode result = root;
        TreeNode temp = new TreeNode(val);
        while (true) {
            if (root == null) {
                result = temp;
                break;
            }
            if (root.val > val) {
                if (root.left == null) {
                    root.left = temp;
                    break;
                }
                root = root.left;
                
            } else if (root.val < val) {
                if (root.right == null) {
                    root.right = temp;
                    break;
                }
                root = root.right;
            }
        }
        return result;

作者:LeetCode
链接:leetcode.cn/leetbook/re…
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。\