求一棵二叉树的宽度

48 篇文章 0 订阅
46 篇文章 0 订阅
  1. 方式一

利用hashmap记录每一个节点所在的层,利用队列进行宽度遍历

hashmap
private int getMaxWidth1(Node head){
	int curMax = 0;
    int curLevel = 1;
    int curWidth = 0;
    HashMap<Node, Integer> map = new HashMap<>();
    map.put(head,1);
    LinkedList<Node> queue = new LinkedList<>();
    queue.add(head);
    while (!queue.isEmpty()){
        Node node = queue.poll();
        if (null != node.left){
            queue.add(node.left);
            map.put(node.left, map.get(node)+1);
        }
        if (null != node.right){
            queue.add(node.right);
           map.put(node.right, map.get(node)+1);
        }
        if (map.get(node)>curLevel){
            curWidth = 1;
            curLevel = map.get(node);
        } else {
            curWidth++;
        }
        curMax = Math.max(curMax, curWidth);
    }
    return curMax;
}

  1. 方式二

利用队列进行宽度遍历,同时记录当前行的最后一个结点,以及下一行的最后一个结点,用于控制结束

不用hashmap
private static int getMaxWidth2(Node head){
	Node nextEnd = null;
    int curNodeCount = 0;
    int maxWidth = 0;
    LinkedList<Node> queue = new LinkedList<>();
    queue.add(head);
    Node curEnd = head;
    while (!queue.isEmpty()){
        Node node = queue.poll();
        if (null != node.left){
            queue.add(node.left);
            nextEnd = node.left;
        }
        if (null != node.right){
            queue.add(node.right);
            nextEnd = node.right;
        }
        curNodeCount++;
        if (node == curEnd){
            maxWidth = Math.max(maxWidth, curNodeCount);
            System.out.println(curNodeCount + "===" + maxWidth);
            curEnd = nextEnd;
            nextEnd = null;
            curNodeCount= 0;

        }
    }
    return maxWidth;
}
完整代码
public class TreeMaxWidth {

  public static class Node{
      private int value;
      private Node left;
      private Node right;

      public Node(int value) {
          this.value = value;
      }
  }

  private static int getMaxWidth(Node head){
      int curMax = 0;
      int curLevel = 1;
      int curWidth = 0;
      HashMap<Node, Integer> map = new HashMap<>();
      map.put(head,1);
      LinkedList<Node> queue = new LinkedList<>();
      queue.add(head);
      while (!queue.isEmpty()){
          Node node = queue.poll();
          if (null != node.left){
              queue.add(node.left);
              map.put(node.left, map.get(node)+1);
          }
          if (null != node.right){
              queue.add(node.right);
             map.put(node.right, map.get(node)+1);
          }
          if (map.get(node)>curLevel){
              curWidth = 1;
              curLevel = map.get(node);
          } else {
              curWidth++;
          }
          curMax = Math.max(curMax, curWidth);
      }
      return curMax;
  }

  private static int getMaxWidth2(Node head){

      Node nextEnd = null;
      int curNodeCount = 0;
      int maxWidth = 0;
      LinkedList<Node> queue = new LinkedList<>();
      queue.add(head);
      Node curEnd = head;
      while (!queue.isEmpty()){
          Node node = queue.poll();
          if (null != node.left){
              queue.add(node.left);
              nextEnd = node.left;
          }
          if (null != node.right){
              queue.add(node.right);
              nextEnd = node.right;
          }
          curNodeCount++;
          if (node == curEnd){
              maxWidth = Math.max(maxWidth, curNodeCount);
              System.out.println(curNodeCount + "===" + maxWidth);
              curEnd = nextEnd;
              nextEnd = null;
              curNodeCount= 0;

          }
      }
      return maxWidth;
  }

  public static void main(String[] args) {
      Node node = new Node(1);
      node.left = new Node(2);
      node.right = new Node(3);
      node.left.left = new Node(4);
      node.left.right = new Node(5);
      node.right.left = new Node(6);
      node.right.right = new Node(7);
      node.left.left.left = new Node(8);
      node.left.left.right = new Node(9);
      node.left.right.left = new Node(10);
//        int maxWidth = getMaxWidth(node);
//        System.out.println(maxWidth);
      int maxWidth2 = getMaxWidth2(node);
      System.out.println(maxWidth2);
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值