Offer prove safety [] [] are printed upside down binary tree (traversal sequence)

Title: down from the print out of each node of the binary tree, with the layer node from left to right printing.

 

A: create a secondary queue, the root node into the team, added to the output of the vector

  Loop: to team up empty

  If the binary tree node will have left the team left node

  If the binary tree node will have the right team into the right node

  The head elements a team

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
	    vector<int> ret;
	    ret.clear();
	    if (root == nullptr)
	    {
		    return ret;
	    }
	    queue<TreeNode*> m_deque; 
	    m_deque.push(root);//根节点入队

	    while (!m_deque.empty())
	    {
		    ret.push_back(m_deque.front()->val);// ret存入队头元素
		    if (m_deque.front()->left != nullptr)
		    {
			    m_deque.push (m_deque.front () -> left ); // left node enqueued 
		    } 
		    IF (! m_deque.front () -> right = nullptr a) 
		    { 
			    m_deque.push (m_deque.front () -> right); // right node enqueued 
		    } 
            m_deque.pop (); // head elements dequeue 
	    } 
        return RET; 
    } 
};

  

 

 

Related topics:

  Modified binary tree:

There are an infinite full binary tree, the root node number is 1. Number of left son node i * 2 i number, the right son 2 * i + 1 (left son of the root node, such as 1, 2, 3, the right son of the left son is four, and right son 5.) . Select beef to make a game in this tree, he got two numbers n and k niu from there, he took the numbers 0 niu hope to go down starting from the root, each time you select an edge to move, to reach a node plus or minus the number of this node number of this node. When the k-th node go figure obtained exactly equal to n.
 

Of course there are many such paths. To increase the difficulty, Niu decided to let the numbers tell the beef and the path of least she passed node.
Niu very smart, there must be a problem given all the answers.
Can you help beef it? https://www.nowcoder.com/practice/39953c879b79412b85fbca2ffdeb0a4c

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11432762.html