/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; }};*/classSolution{privateList<Integer>ans=newArrayList<>();publicList<Integer>preorder(Noderoot){dfs(root);returnans;}privatevoiddfs(Noderoot){if(root==null){return;}ans.add(root.val);for(Nodechild:root.children){dfs(child);}}}
/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }};*/classSolution{public:vector<int>preorder(Node*root){vector<int>ans;function<void(Node*)>dfs=[&](Node*root){if(!root){return;}ans.push_back(root->val);for(auto&child:root->children){dfs(child);}};dfs(root);returnans;}};
1 2 3 4 5 6 7 8 910111213141516171819202122
/** * Definition for a Node. * type Node struct { * Val int * Children []*Node * } */funcpreorder(root*Node)(ans[]int){vardfsfunc(*Node)dfs=func(root*Node){ifroot==nil{return}ans=append(ans,root.Val)for_,child:=rangeroot.Children{dfs(child)}}dfs(root)return}
/** * Definition for a Node. * struct Node { * int val; * int numChildren; * struct Node** children; * }; *//** * Note: The returned array must be malloced, assume caller calls free(). */voiddfs(structNode*root,int*ans,int*i){if(!root){return;}ans[(*i)++]=root->val;for(intj=0;j<root->numChildren;j++){dfs(root->children[j],ans,i);}}int*preorder(structNode*root,int*returnSize){int*ans=malloc(sizeof(int)*10000);*returnSize=0;dfs(root,ans,returnSize);returnans;}
/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; }};*/classSolution{publicList<Integer>preorder(Noderoot){if(root==null){returnCollections.emptyList();}List<Integer>ans=newArrayList<>();Deque<Node>stk=newArrayDeque<>();stk.push(root);while(!stk.isEmpty()){Nodenode=stk.pop();ans.add(node.val);List<Node>children=node.children;for(inti=children.size()-1;i>=0;--i){stk.push(children.get(i));}}returnans;}}
/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }};*/classSolution{public:vector<int>preorder(Node*root){if(!root)return{};vector<int>ans;stack<Node*>stk;stk.push(root);while(!stk.empty()){Node*node=stk.top();ans.push_back(node->val);stk.pop();autochildren=node->children;for(inti=children.size()-1;i>=0;--i)stk.push(children[i]);}returnans;}};
1 2 3 4 5 6 7 8 9101112131415161718192021222324
/** * Definition for a Node. * type Node struct { * Val int * Children []*Node * } */funcpreorder(root*Node)(ans[]int){ifroot==nil{return}stk:=[]*Node{root}forlen(stk)>0{node:=stk[len(stk)-1]ans=append(ans,node.Val)stk=stk[:len(stk)-1]children:=node.Childrenfori:=len(children)-1;i>=0;i--{stk=append(stk,children[i])}}return}