题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

import java.util.*;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/

// 这道题要用到 中序遍历
public class Solution {
    public TreeNode head = null, pre = null;
    // head 是最左边的那个节点,pre 是从左往右开始的前一个节点
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null){
            return null;
        }
        inOrderT(pRootOfTree);
        return head;

    }
    public void inOrderT(TreeNode root){
        if(root == null){
            return;
        }
        inOrderT(root.left);
        if(pre == null){
            head = root;
        }else{
            pre.right = root;
        }
        root.left=pre;
        pre = root;
        inOrderT(root.right);

    }
    
    
}

全部评论

相关推荐

迟缓的斜杠青年巴比Q了:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务