In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to analyze python binary tree level traversal, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.
Binary tree classification
107. Hierarchical Traversals of Binary Trees II
topic
Given a binary tree, returns a bottom-up hierarchical traversal of its node values. (i.e. traverse layer by layer from left to right from the layer where the leaf node is located to the layer where the root node is located)
For example:
Given binary tree [3,9,20,null,null,15,7]
3 / \ 9 20 / \ 15 7
Returns its bottom-up hierarchical traversal as:
[ [15,7], [9,20], [3]] Thought breadth first search
Hierarchical traversal of trees can be achieved using breadth-first search. Search from the root node, traverse all nodes of the same level at a time, and store node values for that level using a list
If you want to output node values for each level from top to bottom, it is intuitive to add a list of node values for that level to the end of the result list after traversing a level of nodes. This problem requires output of node values for each level from bottom to top, with a slight modification: After traversing a level of nodes, add a list of node values for that level to the head of the result list
code/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List levelOrderBottom(TreeNode root) { //Results List levelOrder = new LinkedList(); if(root == null){ return levelOrder; } Queue queue = new LinkedList(); queue.offer(root); //breadth search while(! queue.isEmpty()){ List level = new ArrayList(); int size = queue.size(); for(int i = 0; i < size; i++){ TreeNode node = queue.poll(); level.add(node.val); if(node.left != null){ queue.offer(node.left); } if(node.right != null){ queue.offer(node.right); } } //insert to the first position every time levelOrder.add(0,level); } return levelOrder; }} The above content is how to analyze the hierarchical traversal of python binary tree. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.