程序员代码面试指南刷题--第三章.找到二叉树中符合搜索二叉树条件的最大拓扑结构

题目描述
给定一颗二叉树,已知所有节点的值都不一样, 返回其中最大的且符合搜索二叉树条件的最大拓扑结构的大小。
拓扑结构是指树上的一个联通块。
输入描述:

第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。

以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)

ps:节点的编号就是节点的值。

输出描述:

输出一个整数表示满足条件的最大拓扑结构的大小。

示例1

输入

3 2
2 1 3
1 0 0
3 0 0

输出

3

解法一:递归

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        br.readLine();
        TreeNode root = createTree(br);
        int res = bstMaxNode(root);
        System.out.println(res);
    }
    public static int bstMaxNode(TreeNode root){
        if(root==null) return 0;
        int max = maxNode(root,root);
        max = Math.max(bstMaxNode(root.left),max);
        max = Math.max(bstMaxNode(root.right),max);
        return max;
    }
    public static int maxNode(TreeNode root,TreeNode node){
        if(root!=null&&node!=null&&isBst(root,node)){
            return maxNode(root,node.left)+maxNode(root,node.right)+1;
        }
        return 0;
    }
    public static boolean isBst(TreeNode root,TreeNode node){
        if(root==null){
            return false;
        }
        if(root.val == node.val){
            return true;
        }
        return isBst(root.val>node.val?root.left:root.right,node);
    }
    //递归建树
    public static TreeNode createTree(BufferedReader br){
        try{
            String[] ss = br.readLine().trim().split(" ");
            int data = Integer.parseInt(ss[0]);
            int left = Integer.parseInt(ss[1]);
            int right = Integer.parseInt(ss[2]);
            TreeNode root = new TreeNode(data);
            if(left!=0){
                root.left = createTree(br);
            }
            if(right!=0){
                root.right = createTree(br);
            }
            return root;
        }catch(Exception e){
            return null;
        }
    }


}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val){
        this.val = val;
    }
}


解法二:好理解一点

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        br.readLine();
        TreeNode root = createTree(br);
        int res = bstMaxNode(root);
        System.out.println(res);
    }
    public static int bstMaxNode(TreeNode root){
        if(root==null) return 0;
        int max = maxNode(root);
        max = Math.max(bstMaxNode(root.left),max);
        max = Math.max(bstMaxNode(root.right),max);
        return max;
    }
    public static int maxNode(TreeNode root){
        Queue<TreeNode> q = new LinkedList<>();
        if(root.left!=null) q.add(root.left);
        if(root.right!=null) q.add(root.right);
        int count = 1;
        while(!q.isEmpty()){
            TreeNode node = q.poll();
            if(isBst(root,node)){
                count++;
                if(node.left!=null) q.add(node.left);
                if(node.right!=null) q.add(node.right);
            } 
        }
        return count;
    }
    public static boolean isBst(TreeNode root,TreeNode node){
        if(root==null) return false;;
        if(root.val==node.val) return true;
        if(root.val<node.val){
            return isBst(root.right,node);
        }
        return isBst(root.left,node);
    }
   
    //递归建树
    public static TreeNode createTree(BufferedReader br){
        try{
            String[] ss = br.readLine().trim().split(" ");
            int data = Integer.parseInt(ss[0]);
            int left = Integer.parseInt(ss[1]);
            int right = Integer.parseInt(ss[2]);
            TreeNode root = new TreeNode(data);
            if(left!=0){
                root.left = createTree(br);
            }
            if(right!=0){
                root.right = createTree(br);
            }
            return root;
        }catch(Exception e){
            return null;
        }
    }


}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val){
        this.val = val;
    }
}


解法三:O(N) 懒得看。。

发布了189 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44406146/article/details/105505039