{"id":396690,"date":"2021-03-13T09:55:57","date_gmt":"2021-03-13T01:55:57","guid":{"rendered":"http:\/\/4563.org\/?p=396690"},"modified":"2021-03-13T09:55:57","modified_gmt":"2021-03-13T01:55:57","slug":"%e7%bd%91%e6%98%93%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a-bst-%e7%9a%84%e4%b8%ad%e5%ba%8f%e5%89%8d%e9%a9%b1%e8%8a%82%e7%82%b9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=396690","title":{"rendered":"\u7f51\u6613\u9762\u8bd5\u9898\uff1a BST \u7684\u4e2d\u5e8f\u524d\u9a71\u8282\u70b9"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7f51\u6613\u9762\u8bd5\u9898\uff1a BST \u7684\u4e2d\u5e8f\u524d\u9a71\u8282\u70b9               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h3>\u63cf\u8ff0<\/h3>\n<p>\u7ed9\u51fa\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\u4ee5\u53ca\u5176\u4e2d\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u627e\u5230\u8fd9\u4e2a\u8282\u70b9\u5728\u8fd9\u68f5\u6811\u4e2d\u7684\u4e2d\u5e8f\u524d\u9a71\u8282\u70b9\u3002 \u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1<\/h3>\n<pre><code>\u8f93\u5165: root = {2,1,3}, p = 1 \u8f93\u51fa: null <\/code><\/pre>\n<h3>\u6837\u4f8b 2<\/h3>\n<pre><code>\u8f93\u5165: root = {2,1}, p = 2 \u8f93\u51fa: 1 <\/code><\/pre>\n<p>\u7528 while \u5faa\u73af\u6a21\u62df\u9012\u5f52<\/p>\n<pre><code>\/**  * 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 given BST      * @param p: the given node      * @return: the in-order successor of the given node in the BST      *\/     public TreeNode inorderPredecessor(TreeNode root, TreeNode p) {         \/\/ write your code here         TreeNode succ = null;         while (root != null) {             if (p.val &lt;= root.val) {                 root = root.left;             } else {                 if(succ == null || root.val &gt; succ.val){                     succ = root;                 }                 root = root.right;                              }         }         return succ;             } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u8003<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u6613\u9762\u8bd5\u9898\uff1a BST \u7684\u4e2d\u5e8f\u524d\u9a71\u8282&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/396690"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396690"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/396690\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396690"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}