数的高度(java)

之前求树的高度,都是直接用的节点定义的树的结构,这次是给出的数组,其实也没差距太多。层序遍历即可解决。


1、二叉树的树高(默认,输入一个节点有多个孩子节点时,从第3个孩子节点开始,后续节点都被忽略,不作为该节点的孩子节点处理)

1.1、题目描述

现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度

输入描述:

输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,
下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号

输出描述:

输出树的高度,为一个整数
示例1

输入

5
0 1
0 2
1 3
1 4

输出

3
1.2、code:

package schooloffer17;

import java.util.*;

/**
 * @Author: cxh
 * @CreateTime: 17/11/26 21:07
 * @ProjectName: JavaBaseTest
 * <树的高度></>
 */
class Node{
    int parent;
    int self;
    Node(int parent,int self){
        this.parent=parent;
        this.self=self;
    }
}
public class DepthOfTree {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n;
        while (scanner.hasNextInt()){
            n=scanner.nextInt();
            int[][] array=new int[n-1][2];
            for(int i=0;i<n-1;i++){
                array[i][0]=scanner.nextInt();
                array[i][1]=scanner.nextInt();
            }
            Arrays.sort(array, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    if(o1[0]==o2[0]){
                        return o1[1]-o2[1];
                    }else{
                        return o1[0]-o2[0];
                    }
                }
            });
            Queue<Integer>  queue=new LinkedList<Integer>();
            boolean[] isVisited=new boolean[n];
            //层序遍历
            int front=-1,rear=-1;
            int last=0,level=0;
            queue.add(array[0][0]);
            rear++;
            isVisited[array[0][0]]=true;
            int parent;
            while (!queue.isEmpty()){
                parent=queue.poll();
                front++;
                int count=0;//如果一个节点有超过2个孩子,那么后续节点的孩子不再入队,因为题目要求是二叉树.
                for(int i=0;i<n-1 && rear<n-1;i++){
                    boolean is=false;
                    if(parent==array[i][0] && isVisited[array[i][1]]==false){
                        queue.add(array[i][1]);
                        rear++;
                        isVisited[array[i][1]]=true;
                        is=true;//第一次找到后,做标记,下次找不到,则应该break,减少遍历时间
                        count++;
                    }else if(parent!=array[i][0] && is){
                        break;
                    }
                    if(count==2)
                        break;
                }
                if(front==last){
                    level++;
                    last=rear;
                }
            }
            System.out.println(level);
        }
    }
}


2、多叉树的树高

package schooloffer17;

import java.util.*;

/**
 * @Author: cxh
 * @CreateTime: 17/11/26 21:07
 * @ProjectName: JavaBaseTest
 * <树的高度></>
 */
class Node{
    int parent;
    int self;
    Node(int parent,int self){
        this.parent=parent;
        this.self=self;
    }
}
public class DepthOfTree {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n;
        while (scanner.hasNextInt()){
            n=scanner.nextInt();
            int[][] array=new int[n-1][2];
            for(int i=0;i<n-1;i++){
                array[i][0]=scanner.nextInt();
                array[i][1]=scanner.nextInt();
            }
            Arrays.sort(array, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    if(o1[0]==o2[0]){
                        return o1[1]-o2[1];
                    }else{
                        return o1[0]-o2[0];
                    }
                }
            });
            Queue<Integer>  queue=new LinkedList<Integer>();
            boolean[] isVisited=new boolean[n];
            //层序遍历
            int front=-1,rear=-1;
            int last=0,level=0;
            queue.add(array[0][0]);
            rear++;
            isVisited[array[0][0]]=true;
            int parent;
            while (!queue.isEmpty()){
                parent=queue.poll();
                front++;
                for(int i=0;i<n-1 && rear<n-1;i++){
                    boolean is=false;
                    if(parent==array[i][0] && isVisited[array[i][1]]==false){
                        queue.add(array[i][1]);
                        rear++;
                        isVisited[array[i][1]]=true;
                        is=true;//第一次找到后,做标记,下次找不到,则应该break,减少遍历时间
                    }else if(parent!=array[i][0] && is){
                        break;
                    }
                }
                if(front==last){
                    level++;
                    last=rear;
                }
            }
            System.out.println(level);
        }
    }
}


针对下面的测试用例,1输出结果位10,2输出结果位12

291 0 1 0 2 0 3 0 4 2 5 4 6 3 7 7 8 6 9 4 10 9 11 0 12 5 13 5 14 14 15 11 16 0 17 6 18 9 19 13 20 8 21 13 22 15 23 21 24 9 25 17 26 18 27 27 28 26 29 27 30 19 31 4 32 31 33 11 34 10 35 7 36 27 37 22 38 16 39 12 40 24 41 12 42 32 43 2 44 14 45 14 46 6 47 18 48 1 49 12 50 22 51 13 52 15 53 53 54 54 55 53 56 18 57 42 58 25 59 21 60 44 61 7 62 7 63 30 64 64 65 12 66 9 67 42 68 13 69 47 70 34 71 53 72 25 73 26 74 30 75 7 76 51 77 70 78 70 79 8 80 26 81 43 82 41 83 41 84 79 85 58 86 72 87 25 88 57 89 36 90 56 91 40 92 13 93 63 94 6 95 12 96 23 97 17 98 93 99 59 100 67 101 2 102 19 103 93 104 20 105 53 106 55 107 8 108 54 109 83 110 64 111 75 112 25 113 17 114 83 115 84 116 116 117 46 118 62 119 10 120 63 121 23 122 58 123 31 124 92 125 75 126 57 127 31 128 102 129 123 130 129 131 92 132 2 133 128 134 134 135 6 136 130 137 43 138 136 139 68 140 72 141 95 142 32 143 27 144 87 145 10 146 83 147 22 148 71 149 90 150 43 151 71 152 77 153 114 154 50 155 43 156 116 157 20 158 30 159 104 160 149 161 4 162 20 163 111 164 125 165 17 166 92 167 147 168 14 169 4 170 51 171 40 172 117 173 83 174 164 175 118 176 72 177 86 178 120 179 81 180 2 181 116 182 73 183 20 184 143 185 183 186 154 187 126 188 124 189 100 190 8 191 52 192 38 193 32 194 12 195 17 196 171 197 113 198 112 199 142 200 92 201 59 202 119 203 153 204 29 205 33 206 28 207 96 208 97 209 201 210 121 211 12 212 156 213 62 214 204 215 181 216 150 217 104 218 111 219 180 220 69 221 145 222 70 223 214 224 79 225 142 226 14 227 97 228 56 229 60 230 103 231 82 232 181 233 59 234 82 235 59 236 13 237 43 238 31 239 167 240 195 241 28 242 142 243 12 244 27 245 143 246 241 247 3 248 188 249 113 250 128 251 224 252 163 253 206 254 224 255 158 256 156 257 168 258 192 259 218 260 229 261 17 262 233 263 251 264 242 265 174 266 20 267 56 268 194 269 52 270 162 271 269 272 104 273 228 274 79 275 222 276 57 277 131 278 218 279 245 280 167 281 173 282 107 283 239 284 172 285 284 286 233 287 252 288 238 289 95 290

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值