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:
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