This topic DFS used very not confident.(1) The use of recursion is not bold enough to consider the details, should stand higher.(2) After the idea is clarified, notice the change of the state value, which is the state recovery. When the depth first goes to the end, it needs to be returned, and the corresponding state should return. For example, the state data that was put in the stack/vector is going to pop up./** * Definition for binary tree * struct
We need to judge all the paths from the root node to the leaf node, and multiply the nodes on each path from the root node to the leaf node for 10 operations, similar to the conversion of strings to int operations, specific problems see: https://www.nowcoder.com/practice/ 185a87cd29eb42049132aed873273e83?tpid=46tqid=29051rp=1ru=/ta/leetcodeqru=/ta/leetcode/ Question-ranking, the following is solved:
/** *
// Output all the paths from the root node to the leaf node in the binary tree. cpp: defines the entry point of the console application. # Include "stdafx. h "# include
Running result:
operatortree.cpp//operations on trees # include Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Calculating the leaf nodes of binary tree to deepen understanding of the nature of traversal
Hdu3452 remove the smallest edge set from the undirected tree so that any leaf is not connected to the root/min cut, and hdu3452 is connected.
The idea is coming up. The leaf connects to the edge of the sink node. inf ensures that the edge will not be cut, and the root node can be cut to the sink node. Pay attention to the bidirectional Edge building of the undir
Given A binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can SE E ordered from top to bottom.For example:Given The following binary tree, 1 You should return [1, 3, 4] .Using queues, hierarchy traversal, the last node of each layer./*** Definition for a binary tree node. * public class TreeNode {* in
new nodeP = new node;P-> value = I;P-> lchild = NULL;P-> rchild = NULL;// The binary search tree starts from q = root. If it is small, it turns to left, and if it is large, it turns to right. If it has a position, it is inserted.Q = root;While (1 ){// The inserted node is smaller than the Compare value of the node. If the left node is empty, the inserted node is inserted. Otherwise, q = q-> lchild continues to judge.If (p-> value If (q-> lchild)Q = q
/* Function: (1) all leaf nodes of the output binary tree (2) Output all paths from the leaf node to the root node (3) The longest path in the output (2) Author: pussy Date: 2015-11-28 */# include
Recently I checked the ds2480 chip of 1-wire bus, which uses an algorithm that does not use recursion to traverse the leaf node of a binary tree. Here is a summary for your reference.
Because it has not been compiled, the details may be a bit problematic. But the algorithm is correct.
// In g_bflag, the path of the node is saved. If one of the bits in the array is 0, the left subnode of the node is taken a
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/AD/wKiom1esENLzdGt4AACY_uRxu7s509.png "title=" capture. PNG "alt=" Wkiom1esenlzdgt4aacy_urxu7s509.png "/>Data structure of #include This article from "Zero Egg" blog, declined reprint!Find the distance between the maximum and minimal leaf nodes in a binary tree
Directly on the code, and then elaborateData structure Definition:/** * */package servlet;import java.util.arraylist;import java.util.list;/** * @author Lei * */public class Node {private String Text;private listTraversing and saving paths /** * */package servlet;import java.util.arraylist;import java.util.arrays;import java.util.HashMap;import Java.util.iterator;import Java.util.list;import Java.util.map;import Java.util.Stack; /** * @author Lei * */public class Iteratornodetool {mapJava Tra
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.