关于树的一些OJ练习

606. 根据二叉树创建字符串

class Solution {

    public void tree2strChild(TreeNode t, StringBuilder sb) {

        if (t == null) return;

        sb.append (t.val);

        if (t.left == null){

            if (t.right == null) {

                return;

            }else{

                sb.append("()");

            }

        }else{

            sb.append("(");

            tree2strChild(t.left,sb);

            sb.append(")");

        }

        if (t.right == null){

            return;

        }else {

             sb.append("(");

            tree2strChild(t.right,sb);

           sb.append(")");

        }

    }

    

    public String tree2str(TreeNode root) {

        if (root == null) return null;

        StringBuilder sb = new StringBuilder();

        tree2strChild(root,sb);

        return sb.toString();

    }

}


106. 从中序与后序遍历序列构造二叉树

class Solution {

    public int pi = 0;

    public TreeNode buildTreeChild(int [] postorder,int []inorder,int begin,int end) {

        if (begin > end) return null;

        

        TreeNode root = new TreeNode(postorder[pi]);

        int ri = findIndex(inorder,begin,end,postorder[pi]);

        pi --;

        root.right = buildTreeChild(postorder,inorder,ri+1,end);

        root.left = buildTreeChild(postorder,inorder,begin,ri-1);

        

        return root;

    }

    public int findIndex(int []inorder,int begin,int end,int key) {

        for (int i = 0 ; i <= end ; i++) {

            if (inorder[i] == key) {

                return i;

            }

        }

        return -1;

    }

    public TreeNode buildTree(int[] inorder, int[] postorder) {

         pi = postorder.length -1;

        return buildTreeChild(postorder,inorder,0,inorder.length -1);

    }

}

JZ36 二叉搜索树与双向链表(牛客网)

public class Solution {
    TreeNode prev = null;
    public void ConvertChild(TreeNode pCur) {
        if (pCur == null) return;
        ConvertChild(pCur.left);
        pCur.left = prev;
        if (prev != null) {
            prev.right = pCur;
        }
        prev = pCur;
        ConvertChild(pCur.right);
    }
    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null) return null;
        ConvertChild(pRootOfTree);
        TreeNode head = pRootOfTree;
        while(head.left != null) {
            head = head.left;
        }
        return head;
    }
}
236. 二叉树的最近公共祖先

class Solution {

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {

        if (root == null) return null;

        if (root == p || root == q) return root;

        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);

        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);

        if (leftTree != null && rightTree != null) return root;

        if (leftTree != null ) return leftTree;

        if (rightTree != null) return rightTree;

        return null;

        

    }

}

KY11 二叉树遍历

输入:

abc##de#g##f###

输出:

c b e g d f a

import java.util.*;
class TreeNode{
    public char val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(char val) {
        this.val = val;
    }
}    
public class Main{
public static int i = 0;
public static TreeNode createTree(String str) {
    TreeNode root = null;
    if (str.charAt(i) != '#') {
        root = new TreeNode(str.charAt(i));
        i++;
        root.left = createTree(str);
        root.right = createTree(str);
    }else {
        i++;
    }
    return root;
}
public static void inOrdertraversal(TreeNode root) {
    if (root == null) return ;
     inOrdertraversal( root.left);
     System.out.print(root.val+" ");
     inOrdertraversal( root.right);
   }
public static void main(String[]args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine();
        
        TreeNode root = createTree(str);
        inOrdertraversal(root);
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值