首页 > 其他分享 >BST中插入一个节点

BST中插入一个节点

时间:2023-03-16 19:12:06浏览次数:29  
标签:node TreeNode val BST 插入 insertNode root 节点

 

public class Solution {

   public TreeNode insertNode(TreeNode root, TreeNode node) {        // write your code here        if(root == null){           return node;       }       if(root.val > node.val){                   //这个树里面没有重复的数,所以无需考虑root.val == node.val的情况           root.left = insertNode(root.left, node); //待插入值肯定在左右子树的叶子几点上面       }else{           root.right = insertNode(root.right,node);       }        return root;//最后返回的root值为根节点,每次递归后就要返回当前的root值,以备上一层使用,最后返回整个树的根节点    } }

标签:node,TreeNode,val,BST,插入,insertNode,root,节点
From: https://www.cnblogs.com/MarkLeeBYR/p/17223838.html

相关文章