博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode[94]Binary Tree Inorder Traversal
阅读量:4677 次
发布时间:2019-06-09

本文共 1335 字,大约阅读时间需要 4 分钟。

Given a binary tree, return the inorder traversal of its nodes' values.

For example:

Given binary tree {1,#,2,3},

1    \     2    /   3

 

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? 

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:vector
inorderTraversal(TreeNode *root) { vector
res; if(root==NULL)return res; TreeNode *p=root; stack
sta; while(p!=NULL||!sta.empty()) { while(p!=NULL) { sta.push(p); p=p->left; } if(!sta.empty()) { p=sta.top(); sta.pop(); res.push_back(p->val); p=p->right; } } return res;}/*void inorder(TreeNode *root, vector
&res){ if(root==NULL)return; inorder(root->left,res); res.push_back(root->val); inorder(root->right,res); return;} vector
inorderTraversal(TreeNode *root) { vector
res; inorder(root,res); return res; }*/};

 

转载于:https://www.cnblogs.com/Vae1990Silence/p/4281382.html

你可能感兴趣的文章
Neutron 架构 - 每天5分钟玩转 OpenStack(67)
查看>>
详解JS设计模式
查看>>
CPSR寄存器
查看>>
Java基础50题test7—处理字符串
查看>>
保险行业电话外呼型呼叫中心方案
查看>>
自建型呼叫中心
查看>>
input file 文件上传,js控制上传文件的大小和格式
查看>>
Day 6 函数与模块
查看>>
java类中final方法的作用
查看>>
【58同城模拟】第一日。框架。
查看>>
MVC---- DataSet 页面遍历
查看>>
WebApi请求原理
查看>>
[Node.js] node-persist: localStorage on the server
查看>>
jquery.event 研究学习之bind篇
查看>>
LOJ #108. 多项式乘法
查看>>
军事机密(Secret.pas)
查看>>
正向代理和反向代理
查看>>
@RequestParam、@RequestBody和@ModelAttribute区别
查看>>
.net 缓存
查看>>
egret 取消自动连接github
查看>>